package com.bingxue.edu.management.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.bingxue.edu.framework.common.model.PageQuery;
import com.bingxue.edu.framework.common.model.Pagination;
import com.bingxue.edu.framework.common.model.Response;
import com.bingxue.edu.framework.common.util.ValidationUtils;
import com.bingxue.edu.framework.security.ApiResource;
import com.bingxue.edu.management.system.model.entity.File;
import com.bingxue.edu.management.system.model.query.FileQuery;
import com.bingxue.edu.management.system.model.req.FileReq;
import com.bingxue.edu.management.system.model.resp.FileResp;
import com.bingxue.edu.management.system.model.resp.FileStatisticsResp;
import com.bingxue.edu.management.system.model.resp.FileUploadResp;
import com.bingxue.edu.management.system.service.FileService;
import com.mybatisflex.core.paginate.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.x.file.storage.core.FileInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 文件接口
 *
 * @author panbo
 * @since 2024/07/24 08:57
 */
@Tag(name = "文件接口")
@Validated
@RestController
@RequestMapping("/system/file")
@RequiredArgsConstructor
public class FileController {

    private final FileService fileService;

    @Operation(summary = "分页查询列表")
    @ApiResource("system:file:manage")
    @GetMapping("/page")
    public Response<Pagination<FileResp>> page(FileQuery fileQuery, @Validated PageQuery pageQuery) {
        Page<FileResp> page = fileService.page(fileQuery, pageQuery);
        return Response.success(Pagination.of(page));
    }

    @Operation(summary = "查询文件资源统计", description = "查询文件资源统计")
    @SaCheckPermission("system:file:manage")
    @ApiResource("system:file:manage")
    @GetMapping("/statistics")
    public Response<FileStatisticsResp> statistics() {
        return Response.success(fileService.statistics());
    }

    @Operation(summary = "上传文件", description = "上传文件")
    @ApiResource("system:file:manage")
    @PostMapping("/upload")
    public Response<FileUploadResp> upload(@NotNull(message = "文件不能为空") MultipartFile file) {
        ValidationUtils.throwIf(file::isEmpty, "文件不能为空");
        FileInfo fileInfo = fileService.upload(file);
        return Response.success(FileUploadResp.builder().url(fileInfo.getUrl()).build());
    }

    @Operation(summary="更新数据")
    @ApiResource("system:file:manage")
    @PostMapping("/update/{id}")
    public Response<Void> update(@RequestBody @Validated FileReq req, @PathVariable String id) {
        File file = fileService.copyById(req, id);
        fileService.updateById(file);
        return Response.success();
    }

    @Operation(summary="删除数据")
    @ApiResource("system:file:manage")
    @PostMapping("/delete/{ids}")
    public Response<Void> delete(@PathVariable List<String> ids) {
        fileService.delete(ids);
        return Response.success();
    }

}
